<div id="divCustInventories">
    <?php if ($session->read('Auth.User.prefix') == CUST_PREFIX) : // ถ้าเป็นหน้า Home ของ Customer โชว์ search ด้วย?>
        <div class="toolbar">
            <?php
            echo $this->element('toolbar', array('buttons' => array(
                    array('search' => array('controller' => 'users', 'action' => 'popup_search_cust_inventories', 'update' => 'divCustInventories')),
                    array('refresh' => array('controller' => 'users', 'action' => 'cust_inventories', 'action' => 'refresh_cust_inventories', 'update' => 'divCustInventories')))));
            ?>
        </div>
    <?php endif; ?>

    <?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
        <?php echo $this->element('dataNotFound'); ?>
    <? else : // ถ้ามีข้อมูล ?>
        <?php $paginator->options(array('update' => 'divCustInventories', 'indicator' => 'indicator', 'url' => array('controller' => 'users', 'action' => 'cust_inventories', $site_id))); ?>
        <?php echo $this->element('dataCaptionLimit', array('update' => 'divCustInventories', 'action' => 'cust_inventories', 'params' => $site_id)); ?>
        <?php $cur = $paginator->counter(array('format' => '%start%')); ?>
        <table>
            <thead>
                <tr>
                    <th class="no"><?php echo $paginator->sort('No', 'id'); ?></th>
                    <th><?php echo $paginator->sort('Stock Code', 'CustProductSite.stock_code'); ?></th>
                    <th><?php echo $paginator->sort('Vendor Part no.', 'Product.part_no'); ?></th>
                    <th><?php echo $paginator->sort('Description', 'Product.title'); ?></th>
                    <th width="40px"><?php echo $paginator->sort('C/S', 'CustProductSite.current_stock'); ?></th>
                    <th width="40px"><?php echo $paginator->sort('Reserved', 'CustProductSite.reserve_qty'); ?></th>
                    <th width="40px"><?php echo $paginator->sort('Min', 'CustProductSite.min_stock'); ?></th>
                    <th width="40px"><?php echo $paginator->sort('Max', 'CustProductSite.max_stock'); ?></th>
                    <th width="80px" title="Min Date is the day that current stock begin to lower than min stock.">
                        <?php echo $paginator->sort('Min Date', 'CustProductSite.min_stock_date'); ?>
                    </th>
                    <th width="80px" title="Max Date is the day that current stock begin to lower than max stock.">
                        <?php echo $paginator->sort('Max Date', 'CustProductSite.max_stock_date'); ?>
                    </th>
                    <th width="130px" title="Replenish date is the day that vendor could dispatch product to your stock.">
                        <?php echo $paginator->sort('To Replenish Date', 'CustProductSite.replenish_date'); ?>
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php
                $from_vendor = $session->read('Auth.User.Extend.from_supplier') || $session->read('Auth.User.prefix') == SUPPLIER_PREFIX;
                $from_its_user = $session->read('Auth.User.Extend.from_its_user') || $session->read('Auth.User.prefix') == ITS_PREFIX;
                $count = count($this->data)// mode edit
                ?>
                <?php for ($i = 0; $i < $count; ++$i) : ?>
                    <tr class="<?php
            echo $this->data[$i]['CustProductSite']['current_stock'] <= $this->data[$i]['CustProductSite']['min_stock'] ? 'min-stock' :
                    ($this->data[$i]['CustProductSite']['current_stock'] < $this->data[$i]['CustProductSite']['max_stock'] ? 'max-stock' :
                            ($this->data[$i]['CustProductSite']['current_stock'] < $this->data[$i]['CustProductSite']['max_stock'] ? 'max-stock' :
                                    ($this->data[$i]['CustProductSite']['current_stock'] >= $this->data[$i]['CustProductSite']['max_stock'] ? 'green' : '')));
                    ?>">
                        <td class="center"><span><?php echo $i + 1; ?></span></td>
                        <td><?php echo $this->data[$i]['CustProductSite']['stock_code']; ?></td>
                        <td><?php echo $this->data[$i]['CustProductSite']['dispatch_from'] == ITS_PREFIX ? $this->data[$i]['Product']['part_no'] : $this->data[$i]['Product']['supplier_part_no']; ?></td>
                        <td><?php
                echo!empty($this->data[$i]['CustProductSite']['text']) ?
                        $this->data[$i]['CustProductSite']['text'] :
                        $this->data[$i]['Product']['text'];
                    ?></td>
                        <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['current_stock']; ?></td>
                        <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['reserve_qty']; ?></td>
                        <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['min_stock']; ?></td>
                        <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['max_stock']; ?></td>
                        <?php if ($this->data[$i]['CustProductSite']['current_stock'] >= $this->data[$i]['CustProductSite']['max_stock'] && $this->data[$i]['CustProductSite']['current_stock'] > $this->data[$i]['CustProductSite']['min_stock']) : ?>
                            <td colspan="3">
                                <?php if ($from_its_user || $from_vendor) : ?>
                                    <div id="divMaxStockNote<?php echo $i; ?>" class="wide"><?php echo empty($this->data[$i]['CustProductSite']['max_stock_note']) ? '...' : $this->data[$i]['CustProductSite']['max_stock_note']; ?></div>
                                    <?php echo $ajax->editor("divMaxStockNote{$i}", "/cust_product_sites/inplace_editor/max_stock_note/{$this->data[$i]['CustProductSite']['id']}", array('highlightendcolor' => '#7FFF3F', 'formClassName' => 'wide', 'clickToEditText' => 'Click here to edit max stock reason.', 'size' => 30)); ?>
                                <?php else : ?>
                                    <div id="divMaxStockNote<?php echo $i; ?>" class="wide"><?php echo $this->data[$i]['CustProductSite']['max_stock_note']; ?></div>
                                <?php endif; ?>
                            </td>
                        <?php else : ?>
                            <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['min_stock_date']; ?></td>
                            <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['max_stock_date']; ?></td>
                            <td class="text-right">
                                <span id="spanReplenishDate<?php echo $i; ?>"><?php echo $this->data[$i]['CustProductSite']['replenish_date']; ?></span>
                                <?php
                                echo $form->hidden('replenish_date', array('id' => 'replenishDate' . $i));
                                echo $html->link('Replenish date', array('controller' => 'cust_product_sites', 'action' => 'set_replenish_date', $this->data[$i]['CustProductSite']['id']), array('id' => 'linkReplenishDate' . $i, 'class' => 'hide'), null, false);
                                ?>
                            <?php endif; ?>
                            <?php if ($from_its_user || $from_vendor && ($this->data[$i]['CustProductSite']['current_stock'] < $this->data[$i]['CustProductSite']['max_stock'])) : ?>
                                <script type="text/javascript">
                                    $j(function() {
                                        // Event การ set replenish date
                                        var element = '#replenishDate<?php echo $i; ?>';
                                        var span = '#spanReplenishDate<?php echo $i; ?>';
                                        var link = '#linkReplenishDate<?php echo $i; ?>';
                                        $j(element).datepicker($j.extend(datepicker_options, {showOn: 'button', buttonImage: base + '/img/icon/edit.png', buttonImageOnly: true,
                                            onSelect: function(dateText) {
                                                $j(span).update($j(link).attr('href') + '/' + dateText + '');
                                            },
                                            showButtonPanel: true,
                                            closeText: 'Clear'
                                        }));
                                    });
                                </script>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endfor; ?>
            </tbody>
        </table>
        <?php echo $this->element('paging'); ?>
        <div class="caption">
            <span class="min-stock rect"></span> Current Stock is less than or equal Min Stock.
            <span class="max-stock rect"></span> Current Stock is less than Max Stock.
            <span class="green rect"></span> Current Stock is higher than Max Stock.<br />
        </div>

        <?php if ($from_its_user || $from_vendor) : ?>
            <script type="text/javascript">
                $j(function() {
                    // เก็บ element ที่จะ clear element replenish date
                    var clearRDate = {};

                    $j('div[id^=divMaxStockNote]').handover();
                    $j('.ui-datepicker-trigger').handover();
                    $j('.ui-datepicker-trigger').bind('click', function() {
                        clearRDate.link = $j(this).siblings('a').attr('href');
                        clearRDate.span = '#' + $j(this).siblings('span').attr('id');
                    });

                    // ป่มเคลียร์บนปฏิทิน replenish_date clear ค่าวันที่
                    $j('.ui-datepicker-close').live('click', function(event) {
                        $j(clearRDate.span).update(clearRDate.link);
                    });
                });
            </script>
        <?php endif; ?>

    <? endif; ?>
    <?php //debug($this->data);   ?>

</div>